<?php
/**
 * Created by PhpStorm.
 * User: cth
 * Date: 2017/6/13
 * Time: 上午10:13
 */

/***
 * ┌───┐   ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐
 * │Esc│   │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│  ┌┐    ┌┐    ┌┐
 * └───┘   └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘  └┘    └┘    └┘
 * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
 * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
 * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
 * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │   │
 * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
 * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter  │               │ 4 │ 5 │ 6 │   │
 * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤     ┌───┐     ├───┼───┼───┼───┤
 * │ Shift  │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│  Shift   │     │ ↑ │     │ 1 │ 2 │ 3 │   │
 * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
 * │ Ctrl│    │Alt │         Space         │ Alt│    │    │Ctrl│ │ ← │ ↓ │ → │ │   0   │ . │←─┘│
 * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
 */
namespace App\Repositories;

use App\Models\Printer;
use App\Models\ShopClerk;
use App\Tools\CouldPrint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Shop;
use App\Models\ShopInfo;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class PrinterRepository
{
    protected $printer;

    public function __construct(Printer $printer)
    {
        $this->printer = $printer;
    }

    //获取所有的打印机设备
    public function getAllPrinters(Request $request)
    {
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'shop_id.*'=> '商家信息有误',
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        $res = $this->printer::where('shop_id',$request->shop_id)->get(['id','shop_id','department']);
        if ($res->isEmpty()){
            return show(201,'暂无打印机信息');
        }
        return show(200,'信息获取成功',$res);
    }

    // 添加打印机设备
    public function addPrinter(Request $request){
        $rules = [
            'machinecode'=>'required|min:10',
            'machinekey' => 'required|min:10',
            'shop_id'=>'required|integer|exists:shops,id',
            'department'=>'string'
//            'is_useall'=>'required|integer',
//            'priter_number'=>'integer'
        ];
        $messages = [
            'machinecode.*'=> '机器码有误',
            'machinekey.*'=> '密钥有误',
            'shop_id.*'=> '商家不存在',
            'department.*'=>'部门填写有误'
//            'is_useall'=>'是否全部打印',
//            'priter_number'=>'打印数量'
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        $count = Printer::where('machinecode',$request->machinecode)->count();
        if ($count>0){
            return show(202,'已存在，添加打印机失败');
        }
        $cloudPrint = new CouldPrint();
        $add_res = $cloudPrint->addPrint($request->machinecode,$request->machinekey);
        if ($add_res['error'] != '0'){
            return show(202,'添加打印机失败');
        }
//        if ($request->is_useall == '1'){
//            $res = $this->printer::create(\request(['shop_id','machinekey','machinecode','is_useall','priter_number']));
//        }else{
//            $res = $this->printer::create(\request(['shop_id','machinekey','machinecode','is_useall']));
//        }
        $res = $this->printer::create(\request(['shop_id','machinekey','machinecode','department']));
        if (!$res){
            return show(201,'添加打印机失败');
        }
        return show(200,'添加打印机成功');
    }

    // 展示打印机信息
    public function showOnePrinter(Request $request){
        $rules = [
            'id'=>'required|integer|exists:printers,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'id.*'=> '打印机不存在',
            'shop_id.*'=> '商家不存在',
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        $res = $this->printer::find($request->id);
        if (!$res){
            return show(201,'信息获取失败');
        }
        return show(200,'信息获取成功',$res);
    }
    // 编辑打印机信息
    public function editPrinter(Request $request){
        try{
            $rules = [
                'department'=>'required',
                'is_useall' => 'required|in:1,0',
                'id'=>'required|integer|exists:printers,id',
                'cuisine'=>'',
                'priter_number'=>'required|integer',
                'shop_id'=>'required|integer|exists:shops,id',
            ];
            $messages = [
                'id.*'=> '打印机不存在',
                'is_useall.*'=>'你想搞事啊',
                'department.*' => '部门有误',
                'priter_number.*'=>'打印数量填写有误',
                'cuisine.*'=>'菜系分类有误',
                'shop_id.*'=>'商家信息有误'
            ];

            $validator = Validator::make($request->all(), $rules,$messages);
            if ($validator->fails()) {
                return show(201,$validator->errors()->first());
            }
            $res = DB::table('printers')->where('id',$request->id)->update($request->all());
//        dd($request->all());
//        if ($request->is_useall == '1'){
//            $res = DB::table('printers')->where('id',$request->id)->update(\request(['department','priter_number','cuisine','is_useall']));
//
//        }else{
//            $res = DB::table('printers')->where('id',$request->id)->update($request->all());
//
//        }
            return show(200,'修改成功');
        }catch (\Exception $e){
            return show(202,'修改失败');
        }

    }

    public function deletePrinter(Request $request){
        $rules = [
            'id'=>'required|integer|exists:printers,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $messages = [
            'id.*'=> '打印机不存在',
            'shop_id.*'=> '商家不存在',
        ];
        $validator = Validator::make($request->all(), $rules,$messages);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        $printer = Printer::find($request->id);
        $code = $printer->machinecode;
        $cloudPrint = new CouldPrint();
        $add_res = $cloudPrint->delectPrint($code);
        $add_res = json_decode($add_res,true);
        if ($add_res['error'] != '0'){
            return show(202,'删除打印机失败');
        }
        DB::table("printers")->where('shop_id',$request->shop_id)->delete($request->id);
        return show(200,'删除成功');

    }
}
